package com.zuimei.zuimei.fragments;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.activities.Item2DescActivity;
import com.zuimei.zuimei.adapters.Item2RecycleViewItemAdapter;
import com.zuimei.zuimei.base.BaseFragment;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;

import java.util.List;

import butterknife.Bind;

/**
 * Created by Jogger on 2017/1/16.
 * 发现应用viewpager内fragment
 */

public class FindAppItem2Fragment extends BaseFragment implements SwipeRefreshLayout
        .OnRefreshListener {
    @Bind(R.id.rv_item)
    RecyclerView rv_item;
    @Bind(R.id.srl_refresh)
    SwipeRefreshLayout srl_refresh;
    private List<FindAppItem2Bean> mAppList;
    private Item2RecycleViewItemAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    public int getLayoutId() {
        return R.layout.rv_find_app_item;
    }

    @Override
    public void loadData() {
        srl_refresh.setOnRefreshListener(this);
        mLayoutManager = new LinearLayoutManager(mActivity);
        rv_item.setLayoutManager(mLayoutManager);
        new HttpHelper(mActivity).getData(String.format(ConstantUtil.FIND_APP_ITEM2_URL, -1), new
                HttpHelper
                        .OnLoadCompletedListener() {


                    @Override
                    public void onSuccess(String result) {
                        mAppList = ParseFindAppJson.getItem2List(result);
                        mAdapter = new Item2RecycleViewItemAdapter(mActivity);
                        rv_item.setAdapter(mAdapter);
                        mAdapter.loadData(mAppList);
                        mAdapter.setOnItemClickListener(new Item2RecycleViewItemAdapter
                                .OnItemClickListener() {

                            @Override
                            public void onItemClick(View view, FindAppItem2Bean app) {
                                startNewActivity(Item2DescActivity.class, "app", app);
                            }
                        });
                    }

                    @Override
                    public void onFailure() {
                    }
                });
        //加载下一页
        rv_item.addOnScrollListener(new RecyclerView.OnScrollListener() {
            boolean flag = false;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (flag && newState == RecyclerView.SCROLL_STATE_IDLE) {
                    int pos = mAppList.get(mAppList.size() - 1).getPos();//加载下一页的参数
                    new HttpHelper(mActivity).getData(String.format(ConstantUtil
                            .FIND_APP_ITEM2_URL, pos), new
                            HttpHelper
                                    .OnLoadCompletedListener() {
                                @Override
                                public void onSuccess(String result) {
                                    List<FindAppItem2Bean> appList = ParseFindAppJson
                                            .getItem2List(result);
                                    mAppList.addAll(appList);
                                    mAdapter.loadData(mAppList);
                                }

                                @Override
                                public void onFailure() {

                                }
                            });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy>0){
                    srl_refresh.setRefreshing(false);
                }
                int lastVisiableItem = mLayoutManager.findLastVisibleItemPosition();
                int totalItemCount = mLayoutManager.getItemCount();
                flag = (lastVisiableItem + 1 == totalItemCount);
            }
        });
    }

    @Override
    public void onRefresh() {
        new HttpHelper(mActivity).getRefreshData(String.format(ConstantUtil
                .FIND_APP_ITEM2_URL, -1), new HttpHelper
                .OnLoadCompletedListener() {

            @Override
            public void onSuccess(String result) {
                mAppList = ParseFindAppJson.getItem2List(result);
                mAdapter.loadData(mAppList);
                srl_refresh.setRefreshing(false);
            }

            @Override
            public void onFailure() {
                srl_refresh.setRefreshing(false);
            }
        });
    }
}
